#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

static void sig_quit(int);

int main(int argc, char const* argv[])
{
    sigset_t newmask, oldmask, pendmask;

    if (signal(SIGQUIT, sig_quit) == SIG_ERR)
    {
        perror("cannot catch SIGQUIT");
        exit(0);
    }

    sigemptyset(&newmask);
    sigaddset(&newmask, SIGQUIT);
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
    {
        perror("SIG_BLOCK error");
        exit(0);
    }
    printf("SIGQUIT blocked\n");

    if (sigpending(&pendmask) < 0)
    {
        perror("sigpending error");
        exit(0);
    }

    if (sigismember(&pendmask, SIGQUIT))
    {
        perror("\nSIGQUIT pending\n");
        exit(0);
    }

    printf("sleeping 5 seconds.\n");
    sleep(5);
    printf("wake up.And will be unblock SIGQUIT.\n");

    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
    {
        perror("SIG_SETMASK error");
        exit(0);
    }
    printf("SIGQUIT unblocked.\n");
    pause();

    return 0;
}

static void sig_quit(int signo)
{
    printf("caught SIGQUIT\n");
    if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
    {
        printf("cannot reset SIGQUIT.\n");
        exit(0);
    }
    return;
}
